<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['access:access:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['access:access:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete(2)"
          v-hasPermi="['access:access:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="工号" align="center" prop="dw_employee_no" />
      <el-table-column label="部门" align="center" prop="bumen" />
      <el-table-column label="是否在场" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1"
            ><el-tag type="success">在场</el-tag></span
          >
          <span v-else><el-tag type="danger">离场</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="是否统计考勤" align="center" prop="isnot">
        <template slot-scope="scope">
          <span v-if="scope.row.isnot == 1">统计</span>
          <span v-else>不统计</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handlePeople(scope.row)"
            >查看人员</el-button
          > -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-location-information"
            @click="handleRecords(scope.row)"
            >打卡记录</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,2)"
            v-hasPermi="['access:access:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row, 1)"
            v-hasPermi="['access:access:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人员名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入人员名称" />
        </el-form-item>
        <el-form-item label="工号" prop="dwEmployeeNo">
          <el-input v-model="form.dwEmployeeNo" placeholder="请输工号" />
        </el-form-item>
        <el-form-item label="在场状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="在场" :value=Number(1)> </el-option>
            <el-option label="离场" :value=Number(0)> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="bumen">
          <el-select v-model="form.bumen" placeholder="请选择">
            <el-option label="研发部" value="研发部"> </el-option>
            <el-option label="工程部" value="工程部"> </el-option>
            <el-option label="市场部" value="市场部"> </el-option>
            <el-option label="总经办" value="总经办"> </el-option>
            <el-option label="深圳悦诚" value="深圳悦诚"> </el-option>
            <el-option label="生产技术部" value="生产技术部"> </el-option>
            <el-option label="行政部" value="行政部"> </el-option>
            <el-option label="财务部" value="财务部"> </el-option>
            <el-option label="赛为致新" value="赛为致新"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否考勤" prop="isnot">
          <el-select v-model="form.isnot" placeholder="请选择">
            <el-option label="记录" value="1"> </el-option>
            <el-option label="不记录" value="0"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccessPeople, addPeople, updatePeople,getPoeple,delPeople } from "@/api/access/people";
export default {
  name: "Access",
  dicts: ["sys_people_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      value2: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // renyuan总条数
      totalPeople: 0,
      totalRecords: 0,
      // 岗位表格数据
      postList: [],
      // 人员表格数据
      peopleList: [],
      recordsList: [],
      idthis:"",
      // 岗位选项
      postOptions: [],
      // 弹出层标题
      title: "",
      time: "",
      time2: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      table: false,
      table2: false,
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        dwEmployeeNo: [{ required: true, message: "不能为空", trigger: "blur" }],
        status: [{ required: true, message: "不能为空", trigger: "blur" }],
        bumen: [{ required: true, message: "不能为空", trigger: "blur" }],
        isnot: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listAccessPeople(this.queryParams).then((response) => {
        console.log("response,responseresponse", response);
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        dwEmployeeNo: undefined,
        status: undefined,
        bumen: undefined,
        isnot: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员";
      this.okokok = 1
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row, this.ids);
      this.reset();
      const id2 = {
        id: row.id,
      };
      getPoeple(id2).then((response) => {
        console.log("response", response);
        this.form = response.data;
        this.open = true;
        this.title = "修改人员";
      });
      this.okokok = 2
      this.idthis = row.id
    },
    /** 查看人员按钮操作 */
    handlePeople(row) {
      (this.recordsQuery2.ip = row.accessIp),
        (this.recordsQuery2.loginname = row.loginname),
        (this.recordsQuery2.password = row.password),
        listPeople(this.recordsQuery2).then((response) => {
          console.log("response", response);
          this.peopleList = response.data.data;
          this.totalPeople = response.data.total;
          this.table = true;
        });
    },
    /** 查看打卡记录按钮操作 */

    /** 提交按钮 */
    submitForm: function () {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.okokok == 2) {
            console.log('修改');
            updatePeople(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log('新增');
            addPeople(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, type) {
      const rowID = [];
      rowID.push(row.id);
      console.log(this.ids);
      if (type === 1) {
        this.$modal
          .confirm('是否确认删除人员？')
          .then(function () {
            return delPeople(rowID);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      } else {
        let that = this;
        this.$modal
          .confirm("是否确认删除选择的数据项？")
          .then(function () {
            return delPeople(that.ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
  },
};
</script>
